package com.appkubes.fintech.restful.application.afu.service.impl;

import com.appkubes.fintech.core.po.FintechUserBankCard;
import com.appkubes.fintech.core.po.FintechUserBaseInfo;
import com.appkubes.fintech.core.po.FintechUserContact;
import com.appkubes.fintech.core.po.FintechUserContactExample;
import com.appkubes.fintech.restful.application.afu.entity.AFuFraudDto;
import com.appkubes.fintech.restful.application.afu.entity.AFuRequestParam;
import com.appkubes.fintech.restful.application.afu.entity.BaseAfuDto;
import com.appkubes.fintech.restful.application.afu.service.AFuService;
import com.appkubes.fintech.restful.application.afu.utils.AFuUtils;
import com.appkubes.fintech.restful.application.credit.dao.CreditDao;
import com.appkubes.fintech.restful.application.user.dao.UserBankCardDao;
import com.appkubes.fintech.restful.application.user.dao.UserBaseInfoDao;
import com.appkubes.fintech.restful.application.user.dao.UserContactDao;
import com.google.gson.Gson;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
@Data
public class AFuServiceImpl implements AFuService {
    @Resource
    CreditDao creditDao;
    @Resource
    UserBaseInfoDao userBaseInfoDao;

    @Resource
    UserBankCardDao userBankCardDao;
    /*
        @Resource
        DataContactDao dataContactDao;*/
    @Resource
    UserContactDao userContactDao;


    @Override
    public BaseAfuDto getAFuData(String userKey) {
        AFuRequestParam param = getParamsByUser(userKey);
        BaseAfuDto resultEntity = AFuUtils.getAfuScore(param);
        log.error("afu: getAFuData====={}", new Gson().toJson(resultEntity));
        return resultEntity;
    }

    @Override
    public AFuFraudDto getAFuFRUADData(String userKey) {
        AFuRequestParam param = getParamsByUser(userKey);
        AFuFraudDto resultEntity = AFuUtils.getAfuFurad(param);
        log.error("afu: getAFuFRUADData====={}", new Gson().toJson(resultEntity));
        return resultEntity;
    }

    private AFuRequestParam getParamsByUser(String userKey) {
        FintechUserBankCard bankCard = userBankCardDao.selectByPrimaryKey(userKey);
        FintechUserBaseInfo user = userBaseInfoDao.selectByPrimaryKey(userKey);
        AFuRequestParam param = new AFuRequestParam();
        param.setName(user.getRealName());
        param.setId_no(user.getIdentityNum());
        param.setMobile(user.getPhone());
        param.setBank_no(bankCard.getBankCardNumber());
        List<AFuRequestParam.ContactsBean> contacts = new ArrayList<>();

        FintechUserContactExample example = new FintechUserContactExample();
        example.createCriteria()
                .andUserKeyEqualTo(userKey);
        List<FintechUserContact> dataContactList = userContactDao.selectByExample(example);

        if (null != dataContactList && dataContactList.size() > 0) {
            dataContactList.forEach(it -> {
                AFuRequestParam.ContactsBean bean = new AFuRequestParam.ContactsBean();
                bean.setContact_mobile(it.getRelativePhone());
                bean.setContact_name(it.getRelativeName());

                if ("父亲".equals(it.getRealtionType()) || "母亲".equals(it.getRealtionType())) {
                    bean.setContact_type("PARENTS");
                } else if ("儿子".equals(it.getRealtionType()) || "女儿".equals(it.getRealtionType())) {
                    bean.setContact_type("CHILD");
                } else if ("配偶".equals(it.getRealtionType())) {
                    bean.setContact_type("SPOUSE");
                } else {
                    bean.setContact_type("FRIEND");
                }
                //opm 阿福分，姓名只有一个字会报错，这边做个适配，如果只有一个字，直接忽略不加该联系人
                if (null != bean.getContact_name() && bean.getContact_name().length() > 1) {
                    contacts.add(bean);
                }

            });
            param.setContacts(contacts);
        }
        param.setAmount_business("2");
        log.error("afu: params====={}", new Gson().toJson(param));
        return param;

    }
}
